package com.hongshu.idle.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongshu.common.core.enums.ConfigKeyEnum;
import com.hongshu.idle.domain.dto.AlipayConfigDTO;
import com.hongshu.idle.domain.dto.SystemConfigDTO;
import com.hongshu.idle.domain.entity.WebAlipayConfig;
import com.hongshu.idle.domain.entity.WebSystemConfig;
import com.hongshu.idle.mapper.sys.SysAlipayConfigMapper;
import com.hongshu.idle.mapper.sys.SysSystemConfigMapper;
import com.hongshu.idle.service.sys.ISysSystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 系统配置关系表 服务实现类
 *
 * @author: hongshu
 */
@Slf4j
@Service
public class SysSystemConfigServiceImpl implements ISysSystemConfigService {

    @Autowired
    private SysSystemConfigMapper systemConfigMapper;
    @Autowired
    private SysAlipayConfigMapper alipayConfigMapper;


    // 获取系统配置
    public SystemConfigDTO getSystemConfig() {
        List<String> configKeys = ConfigKeyEnum.getSystemConfigKeys();
        List<WebSystemConfig> configs = this.getConfigsByKeys(configKeys);

        SystemConfigDTO systemConfig = new SystemConfigDTO();
        for (WebSystemConfig config : configs) {
            ConfigKeyEnum configKey = ConfigKeyEnum.fromKey(config.getConfigKey());
            if (configKey != null) {
                switch (configKey) {
                    case OSS_TYPE:
                        systemConfig.setOssType(config.getConfigValue());
                        break;
                    case SMS_PRIMARY:
                        systemConfig.setSmsPrimary(config.getConfigValue());
                        break;
                    case MQ_TYPE:
                        systemConfig.setMqType(config.getConfigValue());
                        break;
                    case QUERY_PRIMARY:
                        systemConfig.setQueryPrimary(config.getConfigValue());
                        break;
                    case AMAP_ENABLED:
                        systemConfig.setAmapEnabled(config.getConfigValue());
                        break;
                    case BLACKLIST_ENABLED:
                        systemConfig.setBlacklistEnabled("1".equals(config.getConfigValue()));
                        break;
                    case CONTENT_AUDIT_ENABLED:
                        systemConfig.setContentAuditEnabled("1".equals(config.getConfigValue()));
                        break;
                    case PRODUCT_AUDIT_ENABLED:
                        systemConfig.setProductAuditEnabled("1".equals(config.getConfigValue()));
                        break;
                    case USER_AUDIT_ENABLED:
                        systemConfig.setUserAuditEnabled("1".equals(config.getConfigValue()));
                        break;
                }
            }
        }
        return systemConfig;
    }

    // 获取支付宝配置
    public AlipayConfigDTO getAlipayConfig() {
        LambdaQueryWrapper<WebAlipayConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("LIMIT 1");
        WebAlipayConfig config = alipayConfigMapper.selectOne(wrapper);

        if (config != null) {
            return AlipayConfigDTO.builder()
                    .pid(config.getPid())
                    .appId(config.getAppId())
                    .merchantPrivateKey(config.getMerchantPrivateKey())
                    .alipayPublicKey(config.getAlipayPublicKey())
                    .notifyUrl(config.getNotifyUrl())
                    .returnUrl(config.getReturnUrl())
                    .signType(config.getSignType())
                    .charset(config.getCharset())
                    .gatewayUrl(config.getGatewayUrl())
                    .isEnabled(config.getIsEnabled())
                    .build();
        }
        return new AlipayConfigDTO();
    }

    // 通用的配置查询方法
    private List<WebSystemConfig> getConfigsByKeys(List<String> configKeys) {
        LambdaQueryWrapper<WebSystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(WebSystemConfig::getConfigKey, configKeys);
        return systemConfigMapper.selectList(wrapper);
    }
}
